#include "stream.h"
#include <iostream>
#include <string>

Stream::Stream(const std::string& address, const bool is_server) {
    context = zmq::context_t();
    if (is_server) {
        stream = zmq::socket_t(context, zmq::socket_type::rep);
        stream.bind(address);
    } else {
        stream = zmq::socket_t(context, zmq::socket_type::req);
        stream.connect(address);
    }
}

void Stream::sendMessage(const std::string& message, const std::string& identity) {
    zmq::message_t msg(message.c_str(), message.size());
    zmq::message_t id(identity.c_str(), identity.size());
    stream.send(id, zmq::send_flags::sndmore);
    stream.send(msg, zmq::send_flags::none);
    std::cout << "Sent: " << message << std::endl;
}

void Stream::receiveMessage() {
    zmq::message_t identity;
    zmq::message_t message;
    stream.recv(identity, zmq::recv_flags::none);
    stream.recv(message, zmq::recv_flags::none);
    std::cout << "Received message from: " << identity.to_string() << std::endl;
    std::cout << "Received: " << message.to_string() << std::endl;
}
